<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>教室管理</title>
    <style>
        /* 整体页面布局 */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
            margin: 0;
        }
        /* 导航栏样式 */
        nav {
            background-color: #333;
            width: 100%;
            max-width: 1800px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 20px;
        }
        nav a {
            color: white;
            text-decoration: none;
            padding: 10px 15px;
            /* 设置导航栏链接之间的距离为 20px */
            margin-right: 20px;
        }
        nav a:hover {
            background-color: #ddd;
            color: black;
        }
        /* 表格容器样式 */
        .table-container {
            background-color: #fff;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 95%;
            max-width: 1800px;
            margin-top: 20px;
        }
        /* 表格样式 */
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
            font-size: 1.1em;
        }
        /* 斑马条纹表格行 */
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        /* 操作列样式 */
        td button {
            display: inline-block;
            padding: 6px 12px;
            background-color: #007BFF;
            color: white;
            text-decoration: none;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        td button:hover {
            background-color: #0056b3;
        }
        /* 筛选表单样式 */
        .filter-form {
            background-color: #fff;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
            width: 95%;
            max-width: 1800px;
            margin-top: 30px;
        }
        .filter-form label {
            margin-right: 10px;
        }
        .filter-form input[type="text"],.filter-form input[type="date"] {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 3px;
        }
        .filter-form input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .filter-form input[type="submit"]:hover {
            background-color: #45a049;
        }
        /* 新增教室按钮样式 */
        .add-classroom-button {
            display: inline-block;
            padding: 10px 20px;
            background-color: #007BFF;
            color: white;
            text-decoration: none;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            margin-bottom: 20px;
        }
        .add-classroom-button:hover {
            background-color: #0056b3;
        }
    </style>
    <!-- 或者使用最新的 jQuery 版本 -->
    <script src="${pageContext.request.contextPath}/js/jquery-3.7.1.min.js"></script>
    <script>
        function showAddClassroomDialog() {
            // 显示新增教室的对话框，可以使用模态框等，这里简单使用 prompt 作为示例
            const classroomName = prompt("请输入教室名称:");
            const building = prompt("请输入教学楼:");
            const description = prompt("请输入教室描述:");
            const capacity = prompt("请输入教室容量:");
            if (classroomName && building && description && capacity) {
                $.ajax({
                    type: "POST",
                    url: "/admin/addClassroom",
                    data: {
                        classroomName: classroomName,
                        building: building,
                        description: description,
                        capacity: capacity
                    },
                    success: function() {
                        // 刷新页面或更新教室信息列表
                        location.reload();
                    },
                    error: function() {
                        alert("添加教室请求失败，请稍后重试。");
                    }
                });
            }
        }
        function editClassroom(classroomId, classroomName, building, description, capacity) {
            var newClassroomName = prompt("请输入新的教室名称:", classroomName);
            var newBuilding = prompt("请输入新的教学楼:", building);
            var newDescription = prompt("请输入新的教室描述:", description);
            var newCapacity = prompt("请输入新的教室容量:", capacity);

            if (newClassroomName && newBuilding && newDescription && newCapacity) {
                $.ajax({
                    type: "POST",
                    url: "/admin/updateClassroom",
                    data: {
                        classroomId: classroomId,
                        classroomName: newClassroomName,
                        building: newBuilding,
                        description: newDescription,
                        capacity: newCapacity
                    },
                    success: function() {
                        // 刷新页面或更新教室信息列表
                        location.reload();
                    },
                    error: function() {
                        alert("更新教室请求失败，请稍后重试。");
                    }
                });
            }
        }
        function deleteClassroom(classroomId) {
            if (confirm("确定要删除这个教室吗？")) {
                $.ajax({
                    type: "POST",
                    url: "/admin/deleteClassroom",
                    data: {
                        classroomId: classroomId
                    },
                    success: function() {
                        // 刷新页面或更新教室信息列表
                        location.reload();
                    },
                    error: function() {
                        alert("删除教室请求失败，请稍后重试。");
                    }
                });
            }
        }
    </script>
</head>
<body>
<nav>
    <div>
        <a href="/reservations">首页</a>
        <a href="/admin/classroomManagement">教室管理</a>
        <a href="#">用户管理</a>
<%--        用户管理跟教室管理是一样的操作，只不过是表名不一样而已--%>
    </div>
    <div>
        <a href="/classroomList1">退出登录</a>
    </div>
</nav>
<div>
    <button class="add-classroom-button" onclick="showAddClassroomDialog()">新增教室</button>
</div>
<form class="filter-form" action="${pageContext.request.contextPath}/classroomManagement" method="get">
    <div>
        <label for="classroomName">教室名称:</label>
        <input type="text" id="classroomName" name="classroomName">
    </div>
    <div>
        <label for="building">教学楼:</label>
        <input type="text" id="building" name="building">
    </div>
    <div>
        <label for="capacity">容量:</label>
        <input type="text" id="capacity" name="capacity">
    </div>
    <div>
        <label for="description">描述:</label>
        <input type="text" id="description" name="description">
    </div>
    <div>
        <input type="submit" value="筛选">
    </div>
</form>
<div class="table-container">
    <table border="1px">
        <tr>
            <th>教室名称</th>
            <th>教学楼</th>
            <th>容量</th>
            <th>描述</th>
            <th>操作</th>
        </tr>
        <c:forEach var="classroom" items="${classroomsList}">
            <tr>
                <td>${classroom.classroomName}</td>
                <td>${classroom.building}</td>
                <td>${classroom.capacity}</td>
                <td>${classroom.description}</td>
                <td>
                    <button onclick="editClassroom(${classroom.classroomId}, '${classroom.classroomName}', '${classroom.building}', '${classroom.description}', ${classroom.capacity})">编辑</button>
                    <button onclick="deleteClassroom(${classroom.classroomId})">删除</button>
                </td>
            </tr>
        </c:forEach>
    </table>
</div>
</body>
</html>